package com.javaxiaobang.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 功    能：解决github的webhook消息总线刷新失败的问题
 * 作    者：tianqiang
 * 时    间：2019/11/17
 */
@Slf4j
@RestController
public class RefreshController {

    @Value("${server.busrefresh}")
    private String busRefresh;

    @Value("${eurekaserver.user}")
    private String username;

    @Value("${eurekaserver.password}")
    private String password;

    @RequestMapping(value = "/refresh")
    public String refresh() {
        RestTemplate restTemplate = new RestTemplate();
//        restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(username, password));//代码增加认证
        HttpHeaders httpHeaders = new HttpHeaders();
//        # 设置content_type为json要不然会报415的错误
        httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json");
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(null, httpHeaders);
//        # 以post方法访问真正的刷新链接
        log.info(busRefresh);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(busRefresh, request, String.class);
        return responseEntity.toString();
    }

}